﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;

namespace Transcend.FileUploader.Utility
{
    /// <summary>
    /// 用作应用程序配置
    /// </summary>
    public class DictionaryWraper
    {

		public DictionaryWraper()
		{
			dictionary = new HybridDictionary();
		}

        private HybridDictionary dictionary = null;

        public HybridDictionary Dictionary
        {
            get { return dictionary; }
            set { dictionary = value; }
        }

        public string GetString(string key)
        {
            if (string.IsNullOrEmpty(key)) return string.Empty;
            return ConvertHelper.NullSafeStr(dictionary[key]);
        }

        public int GetInt32(string key, int def)
        {
            return ConvertHelper.ToInt32(GetString(key), def);
        }

        public decimal GetDecimal(string key, decimal def)
        {
            return ConvertHelper.ToDecimal(GetString(key), def);
        }

        public double GetDouble(string key, double def)
        {
            return ConvertHelper.ToDouble(GetString(key), def);
        }

		public static DictionaryWraper FromAppSettings()
		{
			var wrapper = new DictionaryWraper();
			var setting = System.Configuration.ConfigurationManager.AppSettings;
			foreach (var key in setting.AllKeys)
			{
				wrapper.Dictionary.Add(key, setting[key]);
			}

			return wrapper;
		}
    }
}
